<?php

class Search extends Controller {

	function Search(){
		parent::Controller();
		$this->load->scaffolding('verses');
		$this->load->helper('url');
		$this->load->helper('form');
	}


	function index(){

		/* 中文书卷名数组，全名、短名、章数 */
		$strBooks='创世记,创,出埃及记,出,利未记,利,民数记,民,申命记,申,约书亚记,书,士师记,士,路得记,得,撒母耳记上,撒上,撒母耳记下,撒下,列王记上,王上,列王记下,王下,历代志上,代上,历代志下,代下,以斯拉记,拉,尼希米记,尼,以斯帖记,斯,约伯记,伯,诗篇,诗,箴言,箴,传道书,传,雅歌,歌,以赛亚书,赛,耶利米书,耶,耶利米哀歌,哀,以西结书,结,但以理书,但,何西阿书,何,约珥书,珥,阿摩司书,摩,俄巴底亚书,俄,约拿书,拿,弥迦书,弥,那鸿书,鸿,哈巴谷书,哈,西番雅书,番,哈该书,该,撒迦利亚,亚,玛拉基书,玛,马太福音,太,马可福音,可,路加福音,路,约翰福音,约,使徒行传,徒,罗马书,罗,哥林多前书,林前,哥林多后书,林后,加拉太书,加,以弗所书,弗,腓立比书,腓,歌罗西书,西,帖撒罗尼迦前书,帖前,帖撒罗尼迦后书,帖后,提摩太前书,提前,提摩太后书,提后,提多书,多,腓利门书,门,希伯来书,来,雅各书,雅,彼得前书,彼前,彼得后书,彼后,约翰一书,约一,约翰二书,约二,约翰三书,约三,犹大书,犹,启示录,启';
			$strBooksCNum = Array(50,40,27,36,34,24,21,4,31,24,22,25,29,36,10,13,10,42,150,31,12,8,66,52,5,48,12,14,3,9,1,4,7,3,3,3,2,14,4,28,16,24,21,28,16,16,13,6,6,4,4,5,3,6,4,3,1,13,5,5,3,5,1,1,1,22);
		$aryBooksTemp=explode(',',$strBooks);
		
		for ($i = 1; $i <= 66; $i++) {
			$aryBooks[$i]=array('fn'=>$aryBooksTemp[($i-1)*2],'sn'=>$aryBooksTemp[($i-1)*2+1]
			,'c'=>$strBooksCNum[$i-1]
			);
		}

		srand((double)microtime()*1000000);

		$intBookCount = 66;
//		$intBookNum = 1;
		$intBookNum = rand(1,$intBookCount);
//		$intChapterCount = 50;
		$intChapterCount = $aryBooks[$intBookNum]['c'];
//		$intChapterNum = 1;
		$intChapterNum = rand(1,$intChapterCount);
		$intVerseNum = 0;
		$strBookName = $aryBooks[$intBookNum]['fn'];

		// 分解书卷字符串  matches 1 书卷 2 章节 4 经句

		if (preg_match('/([^\/\d]*)(\d*)?(\:(\d*)?)?$/', uri_string(), $matches)):
			$matches[1]=trim($matches[1]);
			for ($i = 1; $i <= $intBookCount; $i++) {
				if (in_array($matches[1],$aryBooks[$i])) {
					$intBookNum = $i;
					$strBookName = $matches[1];					
					if (is_numeric($matches[2])) $intChapterNum = $matches[2];
					if (isset($matches[4])) $intVerseNum = $matches[4];
					break;
				}
			}
		endif;

		$intChapterCount = $aryBooks[$intBookNum]['c'];
		
		if ($intChapterNum < 1) $intChapterNum = 1;
		if ($intChapterNum > $intChapterCount) $intChapterNum = $intChapterCount;

		
		$this->db->from('verses');
		$this->db->where('book_num',$intBookNum);
		$this->db->where('chapter_num',$intChapterNum);

		$strSearch = $strBookName.$intChapterNum;

		if ($intVerseNum):
			$this->db->where('verse_num',$intVerseNum);
			$strSearch .= ':'.$intVerseNum;				
		endif;
		$query = $this->db->get();
		
		$data['query']=$query;

		if ($intChapterNum==1) {
			if ($intBookNum==1) {
				$strPrevLink = '';
			} else {
				$strPrevLink = $aryBooks[$intBookNum - 1]['sn'].$aryBooks[$intBookNum - 1]['c'];
			}
		} else {
			$strPrevLink = $aryBooks[$intBookNum]['sn'].($intChapterNum-1);
		}

		if ($intChapterNum==$intChapterCount) {
			if ($intBookNum==$intBookCount) {
				$strNextLink = '';
			} else {
				$strNextLink = $aryBooks[$intBookNum + 1]['sn']. 1;
			}
		} else {
			$strNextLink = $aryBooks[$intBookNum]['sn'].($intChapterNum+1);
		}

		$strThisLink = $aryBooks[$intBookNum]['sn'].$intChapterNum;


		$data['uri_arg'] = $strSearch;
		$data['strPrevLink'] = $strPrevLink;
		$data['strNextLink'] = $strNextLink;
		$data['strThisLink'] = $strThisLink;
		$data['strBookNameFull'] = $aryBooks[$intBookNum]['fn'];
		$data['intChapterNum'] = $intChapterNum;

		$this->load->helper('html');		
		$this->load->view('wolframalpha',$data);


	}

}

?>